/*
 * @(#)ADTransformer.java
 * Date 2013-08-25
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.hl7v3common.transformer;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;

import org.apache.commons.lang3.StringUtils;

import org.hl7.v3.AD;
import org.hl7.v3.AdxpCity;
import org.hl7.v3.AdxpCountry;
import org.hl7.v3.AdxpCounty;
import org.hl7.v3.AdxpPostalCode;
import org.hl7.v3.AdxpState;
import org.hl7.v3.AdxpStreetAddressLine;

import us.hornerscorners.lamppost.hl7v3common.utils.HL7Utils;

import us.hornerscorners.lamppost.model.common.CodedElement;

import us.hornerscorners.lamppost.model.patient.PostalAddress;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;


/**
 *
 * @author jim
 */
@ApplicationScoped
public class ADTransformer extends AbstractTransformer<PostalAddress, AD> {

    /**
     * Constructs ...
     *
     */
    public ADTransformer() {
        super();
    }

    /**
     * Method description
     *
     *
     * @param address
     *
     * @return
     */
    @Override
    public AD transform(PostalAddress address) {

        AD result = new AD();

        CodedElement purpose = address.getPurpose();

        if ((purpose != null) && StringUtils.isNotBlank(purpose.getCode())) {

            result.getUse().add(purpose.getCode());
        }

        // City
        String city = address.getCity();

        if (StringUtils.isNotBlank(city)) {

            AdxpCity adxpCity = new AdxpCity();

            adxpCity.getContent().add(city);

            result.getContent().add(HL7Utils.hl7Factory.createADCity(adxpCity));
        }

        // Country
        String country = address.getCountry();

        if (StringUtils.isNotBlank(country)) {

            AdxpCountry adxpCountry = new AdxpCountry();

            adxpCountry.getContent().add(country);

            result.getContent().add(HL7Utils.hl7Factory.createADCountry(adxpCountry));
        }

        // County
        String county = address.getCounty();

        if (StringUtils.isNotBlank(county)) {

            AdxpCounty adxpCounty = new AdxpCounty();

            adxpCounty.getContent().add(county);

            result.getContent().add(HL7Utils.hl7Factory.createADCounty(adxpCounty));
        }

        // State
        String state = address.getState();

        if (StringUtils.isNotBlank(state)) {

            AdxpState adxpState = new AdxpState();

            adxpState.getContent().add(state);

            result.getContent().add(HL7Utils.hl7Factory.createADState(adxpState));
        }

        // Street Address
        List<String> lines = address.getStreetLines();

        for (String line : lines) {

            AdxpStreetAddressLine adxpLine = new AdxpStreetAddressLine();

            adxpLine.getContent().add(line);

            result.getContent().add(
                HL7Utils.hl7Factory.createADStreetAddressLine(adxpLine));
        }

        // Zip Code
        String zip = address.getZipCode();

        if (StringUtils.isNotBlank(zip)) {

            AdxpPostalCode adxpZip = new AdxpPostalCode();

            adxpZip.getContent().add(zip);

            result.getContent().add(HL7Utils.hl7Factory.createADPostalCode(adxpZip));
        }

        return result;
    }
}
